{extend name="public/layout" /}
{block name="body"}
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">
            <div class="layui-card">
                <table id="admin-table" lay-filter="data-list"></table>
            </div>
        </div>
    </div>
{/block}
{block name="script"}
    <script src="/static/admin/js/action.js?v={$version}"></script>
    <script type="text/html" id="row-handler">
        <a class="layui-btn layui-btn-xs" lay-event="del">删除</a>
        <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
        <input data-id='{{ d.id }}'
                {{#  if(d.status == 1){ }}
               checked
               {{#  } }}
               type="checkbox" lay-filter="update_status" lay-skin="switch" lay-text="通过|失败">
    </script>
    <script type="text/html" id="pic">
        <div id="layer-photos">
            {{#  layui.each(d.imgs, function(index, item){ }}
            <img  style="display: inline-block;" width="20%" height="20%" src={{ item.path }}>
            {{#  }); }}

            {{#  if(d.imgs.length === 0){ }}
            无数据
            {{#  } }}
        </div>
    </script>
    <script type="text/html" id="view-data">
        <div id="layer-photos">
            {{#  if(d.comment_num){ }}
            评论数({{ d.comment_num }})
            {{#  } }}
            {{#  if(d.like_num){ }}
            点赞数({{ d.like_num }})
            {{#  } }}
            浏览数({{ d.pv }})
        </div>
    </script>
    <script>
        layui.use(['table','form'], function(){
            var table = layui.table;
            var form = layui.form;
            $ = layui.$;
            table.render({
                elem: '#admin-table'
                ,url: '{:url('index')}'
                ,page: true
                ,toolbar: '#toolbar'
                ,defaultToolbar:['filter']
                ,cols: [[
                    {field: 'id', width:60,title: 'ID'}
                    ,{field: 'detail',width:250, title: '详情'}
                    ,{field: 'user',width:160, title: '发布人'}
                    ,{field: 'view-data', title: '访问数据',templet:'#view-data'}
                    ,{field: 'imgs', title: '图片',templet:'#pic'}
                    ,{field: 'add_time',width:160, title: '添加时间'}
                    ,{field: 'update_time',width:160, title: '更新时间'}
                    ,{field: 'handler', title: '操作',toolbar:'#row-handler'}
                ]]
                ,parseData: function(res){
                    return {
                        "code": res.code,
                        "msg": res.message,
                        "count": res.data.total,
                        "data": res.data.data
                    };
                }
                ,response: {
                    statusCode: 10000
                }
            });

            table.on('tool(data-list)', function(obj){
                var data = obj.data;
                var layEvent = obj.event;
                var tr = obj.tr;
                if(layEvent === 'detail'){
                    window.location.href = "{:url('detail')}?id="+data.id;
                } else if(layEvent === 'del'){
                    layer.confirm('是否确定删除该信息？', function(index){
                        var param = new Object();
                        param.id = data.id;
                        del($,'{:url('del')}',param,function(){
                            obj.del();
                            layer.close(index);
                        })
                    });
                }
            });
            form.on('switch(update_status)', function(data){
                var now = $(this).prop('checked');
                var url = "{:url('update_status')}";
                var d = new Object();
                var _this = this;
                d.id = $(this).attr('data-id');
                if(now){
                    d.status = 1;
                }else{
                    d.status = 0;

                }
                $.post(url,d,function(data){
                   if(data.code != 10000){
                       if(now){
                           $(_this).removeAttr('checked');
                       }else{
                           $(_this).attr('checked',true);

                       }
                       layer.msg(data.msg,{icon:2,time:1500})
                   }
                    form.render('checkbox');
                });

            });

        });
    </script>
{/block}